<template>
    <view class="login">
        <!-- <navigation-custom /> -->
        <view class="content">
            <!-- 头部logo -->
            <view class="header">
                <image :src="logoImage"></image>
            </view>
            <!-- 主体表单 
            <view class="main">
                <wInput v-model="phoneData" type="text" maxlength="11" placeholder="用户名/电话"></wInput>
                <wInput v-model="passData" type="password" maxlength="11" placeholder="密码"></wInput>
            </view> -->

            <!-- <wButton text="登 录" :rotate="isRotate" @click.native="startLogin()"></wButton> -->
            <wButton v-if="loginState===0" text="抖音授权" @click.native="showDouyinOpenAuth()"></wButton>
            <wButton v-if="loginState===1" text="已完成抖音授权" @click.native="back()"></wButton>

            <!-- 其他登录 
            <view class="other_login cuIcon">
                <view class="login_icon">
                    <view class="cuIcon-weixin" @tap="login_weixin"></view>
                </view>
                <view class="login_icon">
                    <view class="cuIcon-weibo" @tap="login_weibo"></view>
                </view>
                <view class="login_icon">
                    <view class="cuIcon-github" @tap="login_github"></view>
                </view>
            </view> -->

            <!-- 底部信息 
            <view class="footer">
                <navigator url="forget" open-type="navigate">找回密码</navigator>
                <text>|</text>
                <navigator url="register" open-type="navigate">注册账号</navigator>
            </view> -->
        </view>

        <!-- <view class="cu-list menu" :class="'card-menu margin-top'">
            <view v-if="loginState===0" class="cu-item" :class="'arrow'">
                <button class="cu-btn content" @click="showDouyinOpenAuth">
                    <text class="cuIcon-roundclosefill text-gray"></text>
                    <text class="text-grey">抖音授权</text>
                </button>
            </view>
            <view v-if="loginState===1" class="cu-item" :class="'arrow'">
                <text class="text-grey">已完成抖音授权</text>
            </view>
        </view> -->
    </view>
</template>

<script>
    var _this;
    import wInput from '../../components/watch-login/watch-input.vue' //input
    import wButton from '../../components/watch-login/watch-button.vue' //button

    export default {
        data() {
            return {
                loginState: 0,
                //logo图片 base64
                logoImage: '',
                phoneData: '', //用户/电话
                passData: '', //密码
                isRotate: false, //是否加载旋转
            };
        },
        components: {
            wInput,
            wButton,
        },
        mounted() {
            _this = this;
            //this.isLogin();
            let that = this;
            uni.checkSession({
                success: function(t) {
                    console.log("没过期");
                    that.queryUserInfo();
                },
                fail: function() {
                    console.log("过期了");
                }
            });
        },
        methods: {
            queryUserInfo: function() {
                this.loading = true
                this.$api.userInfo({}).then((res) => {
                    this.loading = false;
                    console.log("接口中用户信息", res)

                    let userInfo = uni.getStorageSync('userInfo');
                    console.log("缓存中用户信息", userInfo);
                    userInfo.memberInfo = res.data;
                    uni.setStorageSync('userInfo', userInfo);

                    if (res.data.isAuthDouyin) {
                        this.loginState = 1;
                    }
                }).catch((err) => {
                    this.loading = false;
                    console.error('request fail', err);
                })
            },
            showDouyinOpenAuth: function() {
                let that = this;
                console.log("抖音授权");
                tt.showDouyinOpenAuth({
                    scopes: {
                        "video.create": 0, // 用户无法取消
                        "video.data": 2,
                        "video.list": 2,
                        "following.list": 0,
                        "fans.list": 0,
                        "micapp.is_legal": 0,
                    },
                    success: function(res) {
                        console.log("授权成功", res);
                        let reqParam = {
                            "authCode": res.ticket
                        }
                        that.$api.authDouyinOpenAuth(reqParam).then((res) => {
                            console.log("保存授权信息成功");
                            that.loginState = 1;
                        }).catch((err) => {
                            console.error("保存授权信息失败", err);
                        });
                    },
                    fail: function(err) {
                        console.error("抖音授权失败", err);
                        uni.showToast({
                            title: err.errMsg,
                            icon: "none"
                        })
                    }
                })
            },
            back() {
                uni.navigateBack({
                    delta: 1
                });
            },
            isLogin() {
                //判断缓存中是否登录过，直接登录
                // try {
                // 	const value = uni.getStorageSync('setUserData');
                // 	if (value) {
                // 		//有登录信息
                // 		console.log("已登录用户：",value);
                // 		_this.$store.dispatch("setUserData",value); //存入状态
                // 		uni.reLaunch({
                // 			url: '../../../pages/index',
                // 		});
                // 	}
                // } catch (e) {
                // 	// error
                // }
            },
            startLogin() {
                //登录
                if (this.isRotate) {
                    //判断是否加载中，避免重复点击请求
                    return false;
                }
                if (this.phoneData.length == "") {
                    uni.showToast({
                        icon: 'none',
                        position: 'bottom',
                        title: '用户名不能为空'
                    });
                    return;
                }
                if (this.passData.length < 5) {
                    uni.showToast({
                        icon: 'none',
                        position: 'bottom',
                        title: '密码不正确'
                    });
                    return;
                }

                console.log("登录成功")

                _this.isRotate = true
                setTimeout(function() {
                    _this.isRotate = false
                }, 3000)
                // uni.showLoading({
                // 	title: '登录中'
                // });
                // getLogin()
                // .then(res => {
                // 	//console.log(res)
                // 	//简单验证下登录（不安全）
                // 	if(_this.phoneData==res.data.username && _this.passData==res.data.password){
                // 		let userdata={
                // 			"username":res.data.username,
                // 			"nickname":res.data.nickname,
                // 			"accesstoken":res.data.accesstoken,
                // 		} //保存用户信息和accesstoken
                // 		_this.$store.dispatch("setUserData",userdata); //存入状态
                // 		try {
                // 			uni.setStorageSync('setUserData', userdata); //存入缓存
                // 		} catch (e) {
                // 			// error
                // 		}
                // 		uni.showToast({
                // 			icon: 'success',
                // 			position: 'bottom',
                // 			title: '登录成功'
                // 		});
                // 		uni.reLaunch({
                // 			url: '../../../pages/index',
                // 		});
                // 	}else{
                // 		_this.passData=""
                // 		uni.showToast({
                // 			icon: 'error',
                // 			position: 'bottom',
                // 			title: '账号或密码错误，账号admin密码admin'
                // 		});
                // 	}
                // 	uni.hideLoading();
                // }).catch(err => {
                // 	uni.hideLoading();
                // })
            }
        }
    }
</script>

<style>
    @import url("../../components/watch-login/css/icon.css");
    @import url("./css/main.css");
</style>
